﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CustomControls;
using Model.EntityModel;
using DataAccess;
using System.Collections.ObjectModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for UpdateRequestingMedicalContent.xaml
    /// </summary>
    public partial class UpdateRequestingMedicalContent : UserControl
    {
        SearchComplaintWindow sc;
        Complaint c = new Complaint();
        Model.EntityModel.RequestingMedical r = new Model.EntityModel.RequestingMedical();
        Actor ActorVictim = new Actor();
        Person victim = new Person();
        GrantDirectorPermissionWindows Director;

        public UpdateRequestingMedicalContent(long IdComplaint)
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - Modificar Solicitud Evaluacion Medica");
            try
            {
                TxtBoxMedicalCenter.AddValitationRule(new BlankValidRule());
                TxtBoxMedicalName.AddValitationRule(new BlankValidRule());
                TxtBoxMedicalPName.AddValitationRule(new BlankValidRule());      
                TxtBoxMedicalCenter.AddValitationRule(new ShortStringRule(3));
                TxtBoxMedicalName.AddValitationRule(new ShortStringRule(3));
                TxtBoxMedicalPName.AddValitationRule(new ShortStringRule(3));
                c.Id = IdComplaint;
                lblCaseNumber.Content = c.Id;
                r = RequestingMedicalDataAccess.GetRequestByIdComplaint(IdComplaint);
                lblCaseDate.Content = r.RegistrationDate;
                ActorVictim = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima", IdComplaint);
                victim = PersonAndActorDataAccess.GetPersonById(ActorVictim.IdPerson);
                LblVictimName.Content = victim.Name+" "+victim.PaternalName+" "+victim.MaternalName;
                TxtBoxMedicalName.Text = r.MedicalName;
                TxtBoxMedicalPName.Text = r.MedicalPaternalName;
                TxtBoxMedicalMName.DeactivateBlankValidation();
                TxtBoxMedicalMName.Text = r.MedicalMaternalName;
               
                TxtBoxMedicalCenter.Text = r.MedicalCenter;
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("No se registro una solicitud medica", "Informacion", MessageBoxButton.OK, MessageBoxImage.Information);
                this.Loaded += KillApp;
            }

        }

        private bool CheckHasErrors()
        {
            bool res = false;
            foreach (var control in Content.Children)
            {
                GroupBox gp = control as GroupBox;
                if (gp != null)
                {
                    foreach (var ctrl in (gp.Content as Grid).Children)
                    {
                        TextBox tb = ctrl as TextBox;
                        if (tb != null)
                        {
                            tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                            if (Validation.GetHasError(tb))
                                res = true;
                        }
                        ValidationComboBox cb = ctrl as ValidationComboBox;
                        if (cb != null)
                        {
                            cb.LostFocusEventHandler(cb, null);
                            if (Validation.GetHasError(cb))
                                res = true;
                        }
                    }
                }
            }
            return res;
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool hasErrors = CheckHasErrors();
                if (!hasErrors)
                {
                    r.MedicalName = TxtBoxMedicalName.Text;
                    r.MedicalPaternalName = TxtBoxMedicalPName.Text;
                    r.MedicalMaternalName = TxtBoxMedicalMName.Text;
                    r.MedicalCenter = TxtBoxMedicalCenter.Text;
                    r.Status = true;
                    r = RequestingMedicalDataAccess.UpdateRequest(r.Id,r);
                    MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("Solicitud de Atencion Medica modificada! Desea imprimir el documento?", "Informacion", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        var items = new ObservableCollection<UserControl>();
                        items.Add(new PrintRequestingMedical(c.Id));
                        var a = new PrintPreviewDialogV2(items);
                        a.ShowDialog();
                    }
                    else
                    {
                        Xceed.Wpf.Toolkit.MessageBox.Show("Puede imprimir la solicitud de atencion medica posteriormente, entrando a Ver Denuncias, click sobre la denuncia selecciona la opción Ver Solicitud de Evaluacion Medica",
                                                            "Información!", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    UIWindowManager.CleanWindow(this);
                }
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Error al Modificar los datos de la solicitud medica", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                UIWindowManager.CleanWindow(this);
            }
        }       

        private void KillApp(object sender, RoutedEventArgs e)
        {
            UIWindowManager.CleanWindow(this);
        }

        public void Charge()
        {
            try
            {
                ActorVictim = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima", c.Id);
                victim = PersonAndActorDataAccess.GetPersonById(ActorVictim.IdPerson);
                LblVictimName.Content = victim.Name.ToString() + " " + victim.PaternalName.ToString() + " " + victim.MaternalName.ToString();
                r.IdComplaint = c.Id;
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Error en al obtener datos de nuestra base de datos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }


        }

        void getComplaintEventHandler(object sender, RoutedEventArgs e)
        {
            try
            {
                c.Id = (sc.Content.Children[0] as SearchComplaint).SelectedComplaintId;
                Charge();
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Ocurrio un error al asignar denuncia", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void btnKeyComplainant_Click(object sender, RoutedEventArgs e)
        {
            Director = new GrantDirectorPermissionWindows();
            bool result = Convert.ToBoolean(Director.ShowDialog());
            if (result)
            {
                sc = new SearchComplaintWindow();
                (sc.Content.Children[0] as SearchComplaint).listViewComplaints.MouseDoubleClick += (getComplaintEventHandler);
                sc.ShowDialog();
            }
            else
                Xceed.Wpf.Toolkit.MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
